package com.shzu.jwc.ty.controller;

import com.shzu.jwc.ty.bean.InvigilatorInfo;
import com.shzu.jwc.ty.bean.PeExamination;
import com.shzu.jwc.ty.bean.PeExaminationClass;
import com.shzu.jwc.ty.bean.PeExaminationTeacher;
import com.shzu.jwc.ty.common.ExportExcelUtils;
import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.common.Page;
import com.shzu.jwc.ty.common.Semester;
import com.shzu.jwc.ty.param.StartandEndDate;
import com.shzu.jwc.ty.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("skilltest2")
@Controller
public class PeExaminationTwocontroller {

	@Autowired
	PeExaminationservice peExaminationservice;
	
	@Autowired
	SystemYearservice systemYearservice;
	
	@Autowired
	UserTeacherInfoservice userTeacherInfoservice;
	
	@Autowired
	InvigilatorInfoservice invigilatorInfoservice;
	
	@Autowired
	PeExaminationClassService peExaminationClassService;

	@Autowired
	ExamWeekService examWeekService;
	
	@RequestMapping("/skilltest.action")
	public String skilltest(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model)
	{
		Page page=new Page(peExaminationservice.countexamination("体育技能二"),pn);
		page.setPageSize(20);
		List<PeExamination> data=peExaminationservice.getexamination(page,"体育技能二");
		List<PeExaminationClass> examinationClass=peExaminationClassService.getclass();
		model.addAttribute("page",page);
		model.addAttribute("data", data);
		model.addAttribute("examinationClass", examinationClass);
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		model.addAttribute("week",examWeekService.byTypegetWeek("技能考试二"));
		return "skilltest2/skilltest";
	}
	
	@RequestMapping("/selectdate.action")
	public String selectdate(@RequestParam("systemYearId") Integer systemYearId,@RequestParam(value="pn",defaultValue="1") Integer pn,Model model,HttpSession session)
	{
		List<PeExaminationClass> examinationClass=peExaminationClassService.getclass();
		if(systemYearId==0)
		{
			Page page=new Page(peExaminationservice.countexamination("体育技能二"),pn);
			page.setPageSize(20);
			List<PeExamination> data=peExaminationservice.getexamination(page,"体育技能二");
			model.addAttribute("data", data);
			model.addAttribute("page",page);
		}
		else
		{
			Page page=new Page(peExaminationservice.countselectexamination(systemYearId,"体育技能二"),pn);
			page.setPageSize(20);
			List<PeExamination> data=peExaminationservice.bydatesearch(systemYearId,page,"体育技能二");
			model.addAttribute("data", data);
			model.addAttribute("page",page);
			session.setAttribute("systemYearId", systemYearId);
		}
		model.addAttribute("examinationClass", examinationClass);
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		model.addAttribute("week",examWeekService.byTypegetWeek("技能考试二"));
		return "skilltest2/skilltest";
	}
	@RequestMapping("/selectweek.action")
	public String selectweek(@RequestParam("systemExamWeeklyId") Integer systemExamWeeklyId,@RequestParam(value="pn",defaultValue="1") Integer pn,Model model,HttpSession session){
		List<PeExaminationClass> examinationClass=peExaminationClassService.getclass();
		if(systemExamWeeklyId==0)
		{
			Page page=new Page(peExaminationservice.countexamination("体育技能二"),pn);
			page.setPageSize(20);
			List<PeExamination> data=peExaminationservice.getexamination(page,"体育技能二");
			model.addAttribute("data", data);
			model.addAttribute("page",page);
		}
		else
		{
			Page page=new Page(peExaminationservice.countweekexamination(systemExamWeeklyId,"体育技能二"),pn);
			page.setPageSize(20);
			List<PeExamination> data=peExaminationservice.selectweek(systemExamWeeklyId,page,"体育技能二");
			model.addAttribute("data", data);
			model.addAttribute("page",page);
			session.setAttribute("systemExamWeeklyName",examWeekService.byIDgetname(systemExamWeeklyId));
			session.setAttribute("systemExamWeeklyId", systemExamWeeklyId);
		}
		model.addAttribute("examinationClass", examinationClass);
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		model.addAttribute("week",examWeekService.byTypegetWeek("技能考试二"));
		return "skilltest2/skilltest";
	}
	
	@ResponseBody
	@RequestMapping("/insertskilltest.action")
	public Msg insertknowledgetest(PeExamination peExamination)
	{
		peExamination.setPeExaminationCode("体育技能二");
		try {
			peExaminationservice.insertPeExamination(peExamination);
			return Msg.success("插入成功").add("systemExamWeeklyId",peExamination.getSystemExamWeeklyId());
		}catch(Exception ex)
		{
			return Msg.error("插入失败,"+ex.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteskilltest.action")
	public Msg deleteskilltest(@RequestParam("id")Integer peExaminationId)
	{
		try {
			peExaminationservice.delete(peExaminationId);
			return Msg.success("取消成功");
		}catch(Exception ex)
		{
			return Msg.error("取消失败,"+ex.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping("/copyone.action")
	public Msg getone(@RequestParam("id")Integer peExaminationId){
		try{
			peExaminationservice.copyone(peExaminationId);
			return Msg.success("操作成功").add("systemExamWeeklyId",peExaminationservice.getone(peExaminationId).getSystemExamWeeklyId());
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("操作失败");
		}
	}
	
	@RequestMapping("/update.action")
	public String update(@RequestParam("id")Integer peExaminationId,Model model)
	{
		model.addAttribute("examinationClass", peExaminationClassService.getclass());
		model.addAttribute("peExamination", peExaminationservice.getone(peExaminationId));
		model.addAttribute("week",examWeekService.byTypegetWeek("技能考试二"));
		return "skilltest2/update";
	}
	
	
	@ResponseBody
	@RequestMapping("/updatefun.action")
	public Msg updatefun(PeExamination peExamination)
	{
		try {
			peExamination.setPeExaminationState(0);
			peExamination.setPeExaminationCode("体育技能二");
			peExaminationservice.update(peExamination);

			return Msg.success("操作成功");
		}catch(Exception ex)
		{
			return Msg.error("操作失败");
		}
	}
	
	@RequestMapping("/searchskilltest.action")
	public String searchskilltest(@RequestParam(value="pn",defaultValue="1") Integer pn,@RequestParam("peExaminationName") String peExaminationName,Model model)
	{
		List<PeExamination> data=peExaminationservice.search(peExaminationName,"体育技能二");
		List<PeExaminationClass> examinationClass=peExaminationClassService.getclass();
		model.addAttribute("data", data);
		model.addAttribute("examinationClass", examinationClass);
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		model.addAttribute("week",examWeekService.byTypegetWeek("技能考试二"));
		return "skilltest2/skilltest";
	}
	
	@ResponseBody
	@RequestMapping("/searchteacher.action")
	public Msg searchteacher(@RequestParam("value") String userTeacherInfoName)
	{
		return Msg.success("操作成功").add("teacherdata", userTeacherInfoservice.serach(userTeacherInfoName));
	}
	
	@ResponseBody
	@RequestMapping("/addinvigilatorinfo.action")
	public Msg addinvigilatorinfo(InvigilatorInfo invigilatorInfo)
	{
		invigilatorInfo.setExamType("技能考试");
		Integer statue=invigilatorInfoservice.insert(invigilatorInfo);
		if(statue>0) {
			
			return Msg.success("插入成功").add("invigilatorInfo", invigilatorInfoservice.selectone(statue));
		}
		else if(statue==-1)
			return Msg.error("已安排过该老师");
		else
			return Msg.error("插入失败");
	}
	
	@ResponseBody
	@RequestMapping("/selectjk.action")
	public Msg selectjk(Integer examId)
	{
		return Msg.success("操作成功").add("data", invigilatorInfoservice.selectbyempleid(examId,"技能考试"));
	}
	
	@ResponseBody
	@RequestMapping("/deletejk.action")
	public Msg deletejk(Integer invigilatorInfoId)
	{
		try
		{
			invigilatorInfoservice.delete(invigilatorInfoId);
			return Msg.success("删除成功");
		}catch(Exception ex)
		{
			return Msg.error("删除失败"+ex.getMessage());
		}
	}

	@RequestMapping("/showprint.action")
	public String showprint(Model model){
		model.addAttribute("data",peExaminationservice.showpint("体育技能二"));
		return "skilltest2/printexam";
	}

	@RequestMapping("/selectshowprint.action")
	public String selectshowprint (StartandEndDate startandEndDate, Model model){

		model.addAttribute("data",peExaminationservice.selectshowpint(startandEndDate,"体育技能二"));
		return "skilltest2/printexam";
	}
	@RequestMapping("/export.action")
	public ModelAndView export(StartandEndDate startandEndDate, HttpServletResponse response){
		List<PeExaminationTeacher> data=peExaminationservice.selectshowpint(startandEndDate,"体育技能二");
		String title="体育技能二考试安排";
		String[] rowsName=new String[]{"考试时间","体育技能考试","考试地点","考试人数","主监考老师","副监考老师"};
		List<Object[]> datalist=new ArrayList<Object[]>();
		Object[] objs=null;
		SimpleDateFormat date=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat time=new SimpleDateFormat("HH:mm");
		for (int i=0;i<data.size();i++){
			PeExaminationTeacher po=data.get(i);
			objs=new Object[rowsName.length];
			objs[0]=date.format(po.getPeExaminationStarTime())+" "+time.format(po.getPeExaminationStarTime())+"-"+time.format(po.getPeExaminationEndTime());
			objs[1]=po.getPeExaminationName();
			objs[2]=po.getPeExaminationPlace();
			objs[3]=po.getPeExaminationUpperLimit();
			objs[4]=po.getNameExp6();
			objs[5]=po.getNameExp7();
			datalist.add(objs);
		}
		ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, datalist,response);
		ex.exportData();
		return null;
	}

	@ResponseBody
	@RequestMapping("/importPe.action")
	public Msg importPe(@RequestParam("pefile") MultipartFile file){
		if(file.isEmpty())
			return Msg.error("文件名不能为空");
		String name=file.getOriginalFilename();
		if(!name.endsWith(".xlsx"))
			return Msg.error("文件类型必须为xlsx");
		try{
			peExaminationservice.importPe(name,file,"体育技能二");
			return Msg.success("导入成功");
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("导入失败");
		}
	}
}
